package org.example.travel_web.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.example.travel_web.dto.PageResult;
import org.example.travel_web.dto.login.AdmRegisterReq;
import org.example.travel_web.dto.login.LoginReq;
import org.example.travel_web.dto.login.LoginResult;
import org.example.travel_web.dto.sysUser.PageReq;
import org.example.travel_web.dto.sysUser.PageResp;
import org.example.travel_web.dto.sysUser.SysUserReq;
import org.example.travel_web.entity.SysUserET;
import org.example.travel_web.exception.AuthException;
import org.example.travel_web.exception.RunException;
import org.example.travel_web.mapper.SysUserMapper;
import org.example.travel_web.security.StpAdminUtil;
import org.example.travel_web.service.SysUserService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 23591
 * @description 针对表【sys_user(系统用户)】的数据库操作Service实现
 * @createDate 2025-01-21 15:56:49
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUserET>
        implements SysUserService {

    @Override
    public LoginResult login(LoginReq req) {
        LambdaQueryWrapper<SysUserET> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserET::getUsername, req.getUsername());
        SysUserET userET = getOne(queryWrapper);
        if (userET == null || !userET.getPassword().equals(req.getPassword())) {
            throw new AuthException("用户名或密码错误");
        }
        StpAdminUtil.login(userET.getId());
        return LoginResult.builder()
                .token(StpAdminUtil.getTokenValue())
                .build();
    }

    @Override
    public void register(AdmRegisterReq req) {
        LambdaQueryWrapper<SysUserET> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserET::getUsername, req.getUsername());
        if (exists(queryWrapper)) {
            throw new AuthException("用户名已存在");
        }
        SysUserET userET = BeanUtil.toBean(req, SysUserET.class);
        if (!save(userET)) {
            throw new AuthException("注册失败");
        }
    }

    @Override
    public void delete(Long[] ids) {
        if (!removeByIds(List.of(ids))) {
            throw new RunException("删除失败");
        }
    }

    @Override
    public PageResult<PageResp> pageResult(PageReq req) {
        LambdaQueryWrapper<SysUserET> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(req.getKeyword())) {
            queryWrapper.like(SysUserET::getUsername, req.getKeyword())
                    .or()
                    .like(SysUserET::getNickname, req.getKeyword());
        }
        queryWrapper.orderByDesc(SysUserET::getCreatedAt);
        Page<SysUserET> page = page(
                new Page<>(req.getPage(), req.getSize()),
                queryWrapper);
        return PageResult.of(page.getTotal(), page.getCurrent(), page.getSize(), BeanUtil.copyToList(page.getRecords(), PageResp.class));
    }

    @Override
    public void add(SysUserReq req) {
        LambdaQueryWrapper<SysUserET> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserET::getUsername, req.getUsername());
        if (exists(queryWrapper)) {
            throw new RunException("用户名已存在");
        }
        SysUserET userET = BeanUtil.toBean(req, SysUserET.class);
        if (!save(userET)) {
            throw new RunException("注册失败");
        }
    }

    @Override
    public void updateSysUser(SysUserReq req) {
        if (req.getId() == null) {
            throw new RunException("用户ID不能为空");
        }
        LambdaQueryWrapper<SysUserET> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserET::getUsername, req.getUsername())
                .ne(SysUserET::getId, req.getId());
        if (exists(queryWrapper)) {
            throw new RunException("用户名已存在");
        }
        SysUserET userET = BeanUtil.toBean(req, SysUserET.class);
        if (!updateById(userET)) {
            throw new RunException("更新失败");
        }
    }

    @Override
    public SysUserET get(Long id) {
        return getById(id);
    }
}




